package com.mhg.socket;

import java.net.*;
//发送端
public class Demo_01_Udp_Send 
{
	public static void main(String[] args) throws Exception
	{
		//1.创建DatagramSocket
		DatagramSocket socket = new DatagramSocket();
		//2.创建DatagramPacket 将数据打包
		String str = "udp 哥们来啦!";
		DatagramPacket packet = new DatagramPacket(
			str.getBytes(),str.getBytes().length,InetAddress.getByName("10.9.152.56"),10000);
		//3.使用DatagramSocket 的 send方法 发送 数据包
		socket.send(packet);
		//4.关闭DatagramSocket(可选)
		socket.close();
		System.out.println("success");
	}
}
//接收端
class Demo_01_Udp_Receive
{
	public static void main(String[] args) throws Exception{
		//1.创建DatagramSocket 
		DatagramSocket socket = new DatagramSocket(10000);
		//2.创建DatagramPacket 接收数据
		byte[] buf = new byte[1024];
		DatagramPacket packet = new DatagramPacket(buf,buf.length);
		//3.使用DatagramSocket的 receive方法 接收 数据包
		socket.receive(packet);
		//将接收到的数据 输出到控制台上
		String s = new String(packet.getData(),0,packet.getLength());
		//获取发送端的ip地址
		String ip = packet.getAddress().getHostAddress();
		System.out.println(ip+",,,,,"+s);
		//4.关闭DatagramSocket(可选)
	}
}